package imgshare.controller;

import imgshare.common.api.CommonResult;
import imgshare.model.Follow;
import imgshare.model.FollowKey;
import imgshare.service.FollowService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    @RequiresAuthentication
    @PostMapping("/follow_switch")
    public CommonResult<?> switchFollow(@RequestBody @Validated FollowKey followKey) {

        Follow follow = followService.find(followKey);
        if (follow == null) {
            followService.createFollowRelation(followKey);
        } else {
            followService.switchFollow(follow);
        }
        return CommonResult.success();
    }

    @GetMapping("/my_follow_ids")
    public CommonResult<?> getMyFollowingUsersIds(@RequestParam Integer userId) {
        List<Integer> ids = followService.findMyFollowingUsersIds(userId);
        return CommonResult.success(ids);
    }
}
